package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.ReceivingAddressActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.FightGroupShop;
import com.meitianhui.convenitenbuy.module.store.contract.GroupShopCarContract;
import com.meitianhui.convenitenbuy.module.store.di.DaggerStoreComponent;
import com.meitianhui.convenitenbuy.module.store.di.StoreModule;
import com.meitianhui.convenitenbuy.module.store.presenter.GroupShopCarPresenter;
import com.meitianhui.convenitenbuy.util.AddressUtil;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import static com.meitianhui.convenitenbuy.R.id.tv_shop_name;

/**
 * Created by Administrator on 2017/12/2.
 */

public class GroupShopCarActivity extends BaseToolbarActivity implements GroupShopCarContract.View{

    private String mInvitationCode;
    private RelativeLayout mRlAddress;
    private TextView mTvTotalPrice;        //合计
    private TextView mTvPrice;
    private TextView mTvStoreName,mTvShopName,mTvShopNumber,mTvShopPrice;
    private TextView mTvName, mTvPhone, mTvAddress;
    private ImageView mIvGood;
    private EditText mEtFeedBack;
    private String price;
    private FightGroupShop mFightShop;
    private List<ConsumerAddress.ConsumerData> consumerDataList;

    @Inject
    GroupShopCarPresenter presenter;


    @Override
    protected int setView() {
        return R.layout.activity_group_shop_car;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerStoreComponent
                .builder()
                .appComponent(App.getInstance().getAppComponent())
                .storeModule(new StoreModule(this))
                .build()
                .inject(this);
        initView();
        initData();
    }


    private void initView() {
        setTbTitle("购物车");
        isEnable(false);
        mIvGood = getViewById(R.id.iv_good);
        mEtFeedBack = getViewById(R.id.et_feed_back);
        mTvTotalPrice = getViewById(R.id.tv_total_price);
        mTvPrice = getViewById(R.id.tv_price);
        mTvShopPrice = getViewById(R.id.tv_shop_price);
        mTvStoreName = getViewById(R.id.tv_store_name);
        mTvShopName = getViewById(tv_shop_name);
        mTvShopNumber = getViewById(R.id.tv_shop_number);
        RxUtils.bindEvents(getViewById(R.id.tv_add),this);
        RxUtils.bindEvents(getViewById(R.id.tv_del),this);
        mRlAddress = getViewById(R.id.rl_address);
        RxUtils.bindEvents(mRlAddress, this);
        RxUtils.bindEvents(getViewById(R.id.tv_pay), this);
        mTvName = getViewById(R.id.tv_name);
        mTvPhone = getViewById(R.id.tv_phone);
        mTvAddress = getViewById(R.id.tv_address);

    }

    private void initData(){
        mInvitationCode = getIntent().getStringExtra("invitation_code");
        presenter.getGroupSaleDetail(mInvitationCode);
    }


    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.rl_address:   //地址
                Intent intentAdd = new Intent(GroupShopCarActivity.this, ReceivingAddressActivity.class);
                startActivity(intentAdd);
                break;
            case R.id.tv_add:
                if (mFightShop == null) {
                    return;
                }
                String s_tv_num = mTvShopNumber.getText().toString();
                int i_tv_num = Integer.parseInt(s_tv_num);
                i_tv_num += 1;
                mTvShopNumber.setText(i_tv_num + "");
                String now_money=mTvTotalPrice.getText().toString().replace("¥","");
                String s_all_money=StringUtils.moneyAdd(mFightShop.detail.retail_price, now_money);
                mTvTotalPrice.setText("¥" + s_all_money);
                mTvPrice.setText("¥" + s_all_money);
                break;

            case R.id.tv_del:
                if(mFightShop==null){return;}
                String number=mTvShopNumber.getText().toString();
                int i_num=Integer.parseInt(number);
                if(i_num<=1){
                    return;
                }
                i_num-=1;
                mTvShopNumber.setText(i_num+"");
                String money=mTvTotalPrice.getText().toString().replace("¥","");
                String all_money=StringUtils.moneySub(money,mFightShop.detail.retail_price);
                mTvTotalPrice.setText("¥" + all_money);
                mTvPrice.setText("¥" + all_money);
                break;

            case R.id.tv_pay:
                if(mFightShop==null){return;}
                if(TextUtils.isEmpty(mFightShop.detail.goods_code)){
                    TipUtils.showToast(App.getInstance(),"伙拼团商品码为空！",3);
                    return;
                }
                Intent intent=new Intent(GroupShopCarActivity.this,GroupShopCarPayActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("fight_shop",mFightShop);
                intent.putExtras(mBundle);
                String remark=mEtFeedBack.getText().toString().trim();
                if(TextUtils.isEmpty(remark)){
                    remark="";
                }
                intent.putExtra("feed_back",remark);
                String payMoney=mTvTotalPrice.getText().toString().replace("¥","");
                intent.putExtra("pay_price",payMoney);
                intent.putExtra("number", mTvShopNumber.getText().toString());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        consumerDataList = InfoProvider.getUserReceivingAddress(this);
        if (consumerDataList != null) {
            if (consumerDataList.size() > 0) {
                for (int i= 0;i<consumerDataList.size();i++){
                    if(TextUtils.equals(consumerDataList.get(i).getIs_major_addr(),"Y")){
                        mTvName.setText(consumerDataList.get(i).getConsignee());
                        mTvPhone.setText(consumerDataList.get(i).getMobile());
                        String area = AddressUtil.getSSQ(this, consumerDataList.get(i).getArea_id());
                        String address = consumerDataList.get(i).getAddress();
                        mTvAddress.setText(area + address);
                    }
                }
            } else {
                mTvName.setText("");
                mTvPhone.setText("");
                mTvAddress.setText("");
            }
        }
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getGroupSaleDetailSuccess(FightGroupShop data) {
        mFightShop = data;
        String url = "";
        Map<String, String> map = data.doc_url;
        if (map != null) {
            String s_pic_info = data.detail.pic_info;
            try {
                JSONArray jsonArray = new JSONArray(s_pic_info);
                if(jsonArray.length()>=2){
                    JSONObject jsonObject = jsonArray.getJSONObject(1);
                    String pid = jsonObject.getString("path_id");
                    if (!TextUtils.isEmpty(pid)) {
                        url = pid;
                    }
                }else if(jsonArray.length()>=1){
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    String pid = jsonObject.getString("path_id");
                    if (!TextUtils.isEmpty(pid)) {
                        url = pid;
                    }
                }
                GlideUtils.setRectImage(this,map.get(url), R.mipmap.wallet_coupon_item_default, R.mipmap.wallet_coupon_item_default,mIvGood);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        mTvStoreName.setText(data.detail.stores_name );
        mTvShopName.setText(data.detail.goods_title);
        price=data.detail.retail_price;
        price= StringUtils.formatMoney(price);
        mTvShopPrice.setText("¥"+price);
        mTvTotalPrice.setText("¥"+price);
        mTvPrice.setText("¥"+price);

    }

    @Override
    public void onFail(int code, String msg) {

    }
}
